#include "ToolForm.h"
#include "ui_ToolForm.h"
#include "src/DataProvider/App/SysMembers.h"
#include <QTimer>
#include "enumfontsome.h"

ToolForm::ToolForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ToolForm)
{
    ui->setupUi(this);
    this->setWindowTitle("工具箱");
    ui->pb_flush->setText(QChar(IconIdentity::icon_circle));

    seam_model = new  QStringListModel;
    cad_model  = new  QStringListModel;

    ui->cb_cadid->setModel(cad_model);

    ui->cb_seamid->setModel(seam_model);

    SysMembers::GetInstance()->moduleInitDone.connect([this](){
        seam_model->setStringList(SysMembers::GetInstance()->seams);
        cad_model->setStringList(SysMembers::GetInstance()->cads);
    });




}

ToolForm::~ToolForm()
{
    delete ui;
}

void ToolForm::on_pb_transe_clicked()
{
    RobotPos pos = RobotPos::instance(ui->lineEdit->text().toStdString());
    int cur = ui->cb_Cur->currentIndex();
    int tar = ui->cb_Tar->currentIndex();
    if(ui->cb_Cur->currentText() == ui->cb_Tar->currentIndex()){
        ui->textBrowser->setText(QString::fromStdString(pos.toStr()));
        return;
    }

    if(5 == cur){   // 如果是工具坐标系
        posInBase(pos,MatrixTranse.first,MatrixTranse.second);    //进行pos坐标轴变换
        pos.tcp = RobotTCP::UPLOAD;
    }else if (0 == cur) {}
    else {
        pos= RobotTCP(cur) > pos;
    }
    switch (tar) {
    case 0:     // Tool
        posInTool(pos,MatrixTranse.first,MatrixTranse.second);
        ui->textBrowser->setText(QString::fromStdString(pos.toStr()));
        break;
    case 1:     // 焊枪头
        pos = pos>RobotTCP::TorchTop;
        ui->textBrowser->setText(QString::fromStdString(pos.toStr()));
        break;
    case 2:     // 跟踪位置
        pos = pos>RobotTCP::CAMERA1;
        ui->textBrowser->setText(QString::fromStdString(pos.toStr()));
        break;
    case 3:     // 法兰
        pos = pos>RobotTCP::FLANGE;
        ui->textBrowser->setText(QString::fromStdString(pos.toStr()));
        break;
    case 4:     // 相机扫描
        pos = pos>RobotTCP::CAMERA;
        ui->textBrowser->setText(QString::fromStdString(pos.toStr()));
        break;
    case 5:     // Base
        ui->textBrowser->setText(QString::fromStdString(pos.toStr()));
        break;
    }


}

void ToolForm::on_pb_flush_clicked()
{
    ui->transeT->setText(QString::fromStdString(MatrixTranse.first.toStr()));
    ui->transeS->setText(QString::fromStdString(MatrixTranse.second.toStr()));
    if(MatrixTranse.first == RobotPos::instance() || MatrixTranse.second == RobotPos::instance() ){
        ui->pb_transe->setEnabled(false);
    }else{
        ui->pb_transe->setEnabled(true);
    }
}

void ToolForm::on_pb_GenToTool_clicked()
{
    auto pp= ui->le_basepoint->text().toStdString();
    MatrixTranse.first = RobotPos::instance(pp);
    ToolForm::on_pb_flush_clicked();
}

void ToolForm::on_pb_GenToBase_clicked()
{
    MOTORDEGRE f;
    f.angle1 = ui->le_motorPoss->text().toDouble(new bool);
    f.angle2 = ui->le_angle1s->text().toDouble(new bool);
    f.angle3 = ui->le_angle2s->text().toDouble(new bool);
    MatrixTranse.second = getPosition(f);
    ToolForm::on_pb_flush_clicked();
}

void ToolForm::on_cb_cadid_currentIndexChanged(const QString &arg1)
{
    if(arg1.isEmpty()){

    }else {
        auto t = SQLHelper::GetBlade()->_p_tr->Get({"CAD"},{"basepoint"},{"cadid","'"+arg1.toStdString()+"'"});
        ui->le_basepoint->setText(t.at(0).toString());
        ui->rb_self->setChecked(true);
    }
}

void ToolForm::on_cb_seamid_currentIndexChanged(const QString &arg1)
{
    if(arg1.isEmpty()){

    }else {
        auto t = SQLHelper::GetBlade()->_p_tr->Get({"SEAMINFO"},{"motorAngle1s","motorAngle2s","motorPoss","cadid"},{"seamid","'"+arg1.toStdString()+"'"});
        ui->le_motorPoss->setText(t.at(2).toString());
        ui->le_angle2s->setText(t.at(1).toString());
        ui->le_angle1s->setText(t.at(0).toString());
        auto cadid = t.at(3).toString();
        auto ct = SQLHelper::GetBlade()->_p_tr->Get({"CAD"},{"basepoint"},{"cadid","'"+cadid.toStdString()+"'"});
        ui->le_basepoint->setText(ct.at(0).toString());
        ui->rb_ma->setChecked(true);
    }

}
